package com.yonyou.ucf.mdf.app.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.yonyou.ucf.mdd.common.model.rule.RuleExecuteResult;
import com.yonyou.ucf.mdd.ext.bill.dto.BillDataDto;
import com.yonyou.ucf.mdd.ext.bill.rule.base.AbstractCommonRule;
import com.yonyou.ucf.mdd.ext.model.BillContext;
import com.yonyou.ypd.bill.basic.service.api.IYpdCommonRul;
import com.yonyou.ypd.bill.basic.vo.RulCtxVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.imeta.biz.base.BizException;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Component("chuCaiSQAuditRule")
public class ChuCaiSQAuditRule implements IYpdCommonRul {




    @Override
    public Object execute(RulCtxVO rulCtxVO, Map<String, Object> params) {
        log.error("出差申请审核推送北森系统，参数{}",params);
        RuleExecuteResult ruleExecuteResult = new RuleExecuteResult();
        BillDataDto billDataDto = (BillDataDto) params.get("param");
        Object requestData = params.get("requestData");
        JSON parse = JSONUtil.parse(requestData);
        List<Long> orderIds = (parse instanceof cn.hutool.json.JSONArray)
                ? ((JSONArray) parse).stream().map(o -> ((JSON) o).getByPath("id")).filter(ObjectUtil::isNotNull).map(String::valueOf).map(Long::parseLong).collect(Collectors.toList())
                : Optional.ofNullable(parse.getByPath("id")).map(String::valueOf).map(Long::parseLong).map(Lists::newArrayList).orElse(null);
        if (CollUtil.isEmpty(orderIds)) {
            throw new BizException("数据不对");
        }

        return ruleExecuteResult;
    }
}
